// B2KDlg.cpp : implementation file
//

#include "stdafx.h"
#include "YL_DEMO_VOIP.h"
#include "B2KDlg.h"
#include "YL_DEMO_VOIPDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CB2KDlg dialog


CB2KDlg::CB2KDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CB2KDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CB2KDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pParent = pParent;
}


void CB2KDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CB2KDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CB2KDlg, CDialog)
	//{{AFX_MSG_MAP(CB2KDlg)
	ON_BN_CLICKED(IDC_SETRING, OnSetring)
	ON_BN_CLICKED(IDC_SETTONE, OnSettone)
	ON_BN_CLICKED(IDC_CHANNEL, OnChannel)
	ON_BN_CLICKED(IDC_SETLED, OnSetled)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CB2KDlg message handlers

void CB2KDlg::OnSetring() 
{
	static BOOL bRing = TRUE;
	
	YL_DeviceIoControl(YL_IOCTL_SET_B2KCURRENTCHANNEL, (LPVOID)CHANNEL_USB);

	YL_DeviceIoControl(YL_IOCTL_SET_RING, (LPVOID)bRing, 0);

	if ( bRing )
		GetDlgItem(IDC_RINGSTATE)->SetWindowText( _T("START_RING") );
	else
		GetDlgItem(IDC_RINGSTATE)->SetWindowText( _T("STOP_RING") );
	
	bRing = ! bRing;
}

void CB2KDlg::OnSettone() 
{
	static int eType = PHONE_TONE_SILENCE;
	
	YL_DeviceIoControl(YL_IOCTL_SET_TONE, (LPVOID)eType);
	
	switch ( eType )
	{
	case PHONE_TONE_UNKNOW:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_UNKNOW") );
		break;
	case PHONE_TONE_SILENCE:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_SILENCE") );
		break;
	case PHONE_TONE_MUTE:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_MUTE") );
		break;
	case PHONE_TONE_DIAL:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_DIAL") );
		break;
	case PHONE_TONE_CONNECTING:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_CONNECTING") );
		break;
	case PHONE_TONE_RINGBACK:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_RINGBACK") );
		break;
	case PHONE_TONE_BUSY:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_BUSY") );
		break;
	default:
		break;
	}
	
	eType++;
	
	if ( eType > PHONE_TONE_BUSY )
		eType = PHONE_TONE_SILENCE;
}

void CB2KDlg::OnChannel() 
{
	short sChannel = 0;
	YL_DeviceIoControl(YL_IOCTL_GET_B2KCURRENTCHANNEL, &sChannel);

	if ( CHANNEL_USB != sChannel )
	{
		YL_DeviceIoControl(YL_IOCTL_SET_B2KCURRENTCHANNEL, (LPVOID)CHANNEL_USB);
		YL_DeviceIoControl(YL_IOCTL_SET_B2KDEFAULTCHANNEL, (LPVOID)CHANNEL_USB);
		GetDlgItem(IDC_CHANNELSTATE)->SetWindowText( _T("CHANNEL_USB") );
	}
	else
	{
		YL_DeviceIoControl(YL_IOCTL_SET_B2KCURRENTCHANNEL, (LPVOID)CHANNEL_PSTN);
		YL_DeviceIoControl(YL_IOCTL_SET_B2KDEFAULTCHANNEL, (LPVOID)CHANNEL_PSTN);
		GetDlgItem(IDC_CHANNELSTATE)->SetWindowText( _T("CHANNEL_PSTN") );
	}
}


void CB2KDlg::OnSetled() 
{
	static int iState = 0;

	YL_DeviceIoControl(YL_IOCTL_SET_B2KLED, (LPVOID)iState);

	CString strNote = _T("");
	strNote.Format( _T("LED State %d"), iState );
	GetDlgItem(IDC_LEDSTATE)->SetWindowText( strNote );

	iState = (++iState)%4;
}


